package com.example.xianji.activity;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main_ShengQing_DuiJieActivity extends Activity implements OnClickListener {
    private RelativeLayout shenqing_duijie_back;
    private Button button_duijie;
    private CheckBox shengqing_duijie_checkbox;
    //    private SharedPreferences sp;
//    private Editor ed;
    private EditText edittext_duijie_details;
    private boolean check = true;

    private TextView xianji_xieyi;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main__sheng_qing__dui_jie);
        //XJApplication.getInstance().addActivity(this);
        shenqing_duijie_back = (RelativeLayout) findViewById(R.id.shenqing_duijie_back);
        button_duijie = (Button) findViewById(R.id.button_duijie);
        edittext_duijie_details = (EditText) findViewById(R.id.edittext_duijie_details);
        shengqing_duijie_checkbox = (CheckBox) findViewById(R.id.shengqing_duijie_checkbox);
        xianji_xieyi = (TextView) findViewById(R.id.xianji_xieyi);

        shengqing_duijie_checkbox.setChecked(true);
        shengqing_duijie_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    check = true;
                } else {
                    check = false;
                }
            }
        });

//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        shenqing_duijie_back.setOnClickListener(this);
        button_duijie.setOnClickListener(this);
        xianji_xieyi.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.shenqing_duijie_back) {
            this.finish();
        } else if (v.getId() == R.id.button_duijie) {
            if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
                if (edittext_duijie_details.getText().toString().length() > 0) {
                    if (check) {
                        get_duijie();
                    } else {
                        Toast.makeText(Main_ShengQing_DuiJieActivity.this, "请先阅读我们的协议", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(Main_ShengQing_DuiJieActivity.this, "请填入详细对接内容", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(Main_ShengQing_DuiJieActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(Main_ShengQing_DuiJieActivity.this, Main_LoginActivity.class));
            }
        } else if (v.getId() == R.id.xianji_xieyi) {
            startActivity(new Intent(Main_ShengQing_DuiJieActivity.this, Main_DuiJie_XieYiActivity.class));
        }
    }

    public void get_duijie() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        map.put("demand_id", getIntent().getStringExtra("xuqiu_id"));
        map.put("details", edittext_duijie_details.getText().toString());
        JSONObject params = new JSONObject(map);

        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.shenqing_duijie,
                params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    Toast.makeText(Main_ShengQing_DuiJieActivity.this, response.getString("msg"), Toast.LENGTH_SHORT).show();
                    if (response.getInt("status") == 1) {
                        startActivity(new Intent(Main_ShengQing_DuiJieActivity.this, Main_My_DuiJieActivity.class));
                        Main_ShengQing_DuiJieActivity.this.finish();
                    }
                    handler.sendEmptyMessage(1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }
}
